package com.yichen;

import com.yichen.entity.ClassFieldInfo;
import com.yichen.entity.JavaTypeEnum;
import com.yichen.entity.TableFieldInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname DataBaseUtil
 * @Description
 * @Author caozhenhao001
 * @Date 2024/5/21
 * @Version
 */

public class DataBaseUtil {

    // JDBC URL, username, and password of MySQL server
    private static final String URL = "jdbc:mysql://localhost:3306/test";
    private static final String USER = "root";
    private static final String PASSWORD = "root";

    // JDBC variables for opening and managing connection
    private static Connection connection;

    private static String SELECT_TABLE_FIELD_SQL = "SHOW COLUMNS FROM ";

    static {
        try {
            // 1. Register JDBC driver
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. Open a connection
            connection = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }


    }

    public static List<ClassFieldInfo> convertTableToClass(List<TableFieldInfo> tableFieldInfoList){

        Map<String, JavaTypeEnum> mysqlToJavaMapping = new HashMap<>(){{
            put("varchar", JavaTypeEnum.STRING);
            put("char", JavaTypeEnum.STRING);
            put("text", JavaTypeEnum.STRING);
            put("json", JavaTypeEnum.STRING);

            put("int", JavaTypeEnum.INTEGER);
            put("bigint", JavaTypeEnum.LONG);
            put("double", JavaTypeEnum.DOUBLE);
            put("datetime", JavaTypeEnum.DATE);
            put("timestamp", JavaTypeEnum.DATE);

        }};

        List<ClassFieldInfo> classFieldInfoList = new ArrayList<>();
        for (TableFieldInfo fieldInfo : tableFieldInfoList) {

            ClassFieldInfo classFieldInfo = new ClassFieldInfo();
            classFieldInfo.setName(Utils.convertSnakeToCamel(fieldInfo.getField()));

            // 去掉int(11)中(11)，然后进行类型匹配
            String cleanType = fieldInfo.getType().replaceAll("\\(.*\\)", "");
            classFieldInfo.setType(mysqlToJavaMapping.getOrDefault(cleanType, JavaTypeEnum.OBJECT));

            classFieldInfoList.add(classFieldInfo);
        }
        return classFieldInfoList;
    }

    public static List<TableFieldInfo> getTableFieldInfo(String tableName) {

        List<TableFieldInfo> tableFieldInfoList = new ArrayList<>();

        String query = SELECT_TABLE_FIELD_SQL + tableName;

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(query);
            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                // Retrieve by column name
                TableFieldInfo fieldInfo =new TableFieldInfo();
                fieldInfo.setField(resultSet.getString("field"));
                fieldInfo.setType(resultSet.getString("type"));
                fieldInfo.setIsNull(resultSet.getString("null"));
                fieldInfo.setKey(resultSet.getString("key"));
                fieldInfo.setExtra(resultSet.getString("extra"));

                tableFieldInfoList.add(fieldInfo);
            }
        } catch (Exception e){
            e.printStackTrace();
        }


        return tableFieldInfoList;
    }



    public static void main(String[] args) throws SQLException {
        System.out.println(getTableFieldInfo("user"));
        System.out.println(Utils.convertSnakeToCamel("user_name"));
        System.out.println(Utils.convertSnakeToCamel("user"));
        System.out.println(Utils.convertSnakeToCamel("user_name_age_page_id_sex"));
    }
}
